home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8041 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.3 KB

  1. Path: colossus.holonet.net!russell
  2. From: russell@news.mdli.com (Russell Blackadar)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: newing char**
  5. Date: 13 Feb 1996 01:59:20 GMT
  6. Organization: HoloNet National Internet Access System: 510-704-1058/modem
  7. Message-ID: <4for9o$iv8@colossus.holonet.net>
  8. References: <4fggal$4pf@darkstar.UCSC.EDU> <4fmgi5$jvv@darkstar.UCSC.EDU>
  9. NNTP-Posting-Host: jubal.mdli.com
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Jonathan Gibbs (jono@cse.ucsc.edu) wrote:
  13. : Thanks for the replies. Unfortunatly I made a few typos in the post.
  14. : The solution was change
  15. :    name = new (char*)[num];
  16. : to
  17. :    name = new char*[num];
  18.  
  19. : Does anyone know if the () are legal, though?
  20.  
  21. The expression on the rhs is legal with parens, but it does not
  22. do what you think!  Let's look at what it means:
  23.  
  24.      new (char*)[num]
  25.  
  26. can be written
  27.  
  28.      new(char*) [num]
  29.  
  30. which is the same as
  31.  
  32.      ( new char* )[num]
  33.  
  34. which is the same as
  35.  
  36.      *(new char* + num)   
  37.  
  38. This ridiculous but legal expression allocates a char* on
  39. the heap, then accesses unallocated storage at a byte offset
  40. num * sizeof(char*) from this heap location.  Mayhem will
  41. ensue!
  42.  
  43. Fortunately, since I believe you declared name with the type 
  44. char**, and the rhs has type char*, the above assignment would
  45. not be legal and you should get a compile error.
  46. --
  47. Russell Blackadar,   russell@mdli.com
  48.